package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.beans.*;
import com.oracle.dao.CheckGroupItemMapper;
import com.oracle.dao.CheckGroupMapper;
import com.oracle.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    CheckGroupMapper checkGroupMapper;
    @Autowired
    CheckGroupItemMapper checkGroupItemMapper;

    @Override
    public ResultInfo findList(QueryPageVo vo) {
        Page<CheckGroup> page= PageHelper.startPage(vo.getCurrentPage(),vo.getPageSize());
        List<CheckGroup> checkGroupList=checkGroupMapper.findList(vo);

        PageBean<CheckGroup> pageBean = new PageBean<>();
        pageBean.setList(page.getResult());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setTotalPage(page.getPages());
        pageBean.setCurrentPage(vo.getCurrentPage());
        pageBean.setPageSize(vo.getPageSize());

        return new ResultInfo(true,"",pageBean);
    }

    /**
     * 检查组的添加操作
     * @param checkGroupData
     * @return
     */
    @Override
    @Transactional
    public ResultInfo add(CheckGroupData checkGroupData) {
        //1.添加基本信息到t_checkgroup
        Integer rows=checkGroupMapper.add(checkGroupData.getCheckGroup());
        //2.添加检查组关联的检查项到t_checkgroup_checkitem
        addCheckGroupRelation(checkGroupData);
        return new ResultInfo(true,"添加检查组成功");
    }

    /**
     * 查询检查组关联的检查项
     * @param id
     * @return
     */
    @Override
    public ResultInfo findCheckItemIds(Integer id) {
        List<Integer> checkItemIds=checkGroupItemMapper.findCheckItemIds(id);
        return new ResultInfo(true,"",checkItemIds);
    }

    @Override
    @Transactional
    public ResultInfo update(CheckGroupData checkGroupData) {
        //        1.修改基本信息到 t_checkGroup
        Integer rows=checkGroupMapper.update(checkGroupData.getCheckGroup());
        //2.修改检查组关联的检查项到t_checkGroup_checkItem
        updateCheckItemRelation(checkGroupData);
        return new ResultInfo(true,"修改检查项成功");
    }

    @Override
    public ResultInfo delete(Integer id) {
        try {
            int rows = checkGroupMapper.delete(id);
            return new ResultInfo(true,"删除检查项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultInfo(false,"删除检查项失败，请检查检查组是否有关联数据");
        }
    }

    private void updateCheckItemRelation(CheckGroupData checkGroupData) {
        //先删除原来关联的检查项
        Integer rows=checkGroupItemMapper.deleteRelation(checkGroupData.getCheckGroup().getId());
        //再添加新关联的检查项
        addCheckGroupRelation(checkGroupData);
    }

    private void addCheckGroupRelation(CheckGroupData checkGroupData) {
        if(checkGroupData.getCheckItemIds()==null || checkGroupData.getCheckItemIds().length==0){
            return;
        }

        Integer checkGroupId = checkGroupData.getCheckGroup().getId();
        for (Integer checkItemId : checkGroupData.getCheckItemIds()) {
            checkGroupItemMapper.add(checkGroupId,checkItemId);
        }
    }
}

